//数值转换

const parser = require("@babel/parser");
const traverse = require("@babel/traverse").default;
const types = require("@babel/types");
const generator = require("@babel/generator").default;

const jscode = `var a = "Hello,AST"["charCodeAt"](5);`; //转换为i var b=3;

const ast = parser.parse(jscode);
const visitor = {
    MemberExpression(path){
        let property=path.get("property");
        if (property.isStringLiteral()){
            let value=property.node.value;
            path.node.computed=false; //原为true,改后的效果把[]变为.
            property.replaceWith(types.Identifier(value)); //把 "charCodeAt" 变为 charCodeAt
        }
    },
    "CallExpression":{
        exit:function (path) {
            const {confident,value}=path.evaluate()
            confident && path.replaceInline(types.valueToNode(value))
        }
    }

}

traverse(ast, visitor);
console.log(generator(ast)['code'])




